package com.opNaga.bpmp.managerTest;

import com.opNaga.bpmp.BPMPElement;
import com.opNaga.bpmp.BPMPManager;
import com.opNaga.bpmp.connectingObject.messageFlow.MessageFlow;
import com.opNaga.bpmp.connectingObject.sequenceFlow.SequenceFlow;
import com.opNaga.bpmp.flowObject.activity.Task;
import com.opNaga.bpmp.swimlane.Pool;
import org.junit.Assert;
import org.junit.Test;

import java.util.Iterator;

/**
 * User: Junyi BEI
 * Date: 01/02/2012
 * Time: 12:52
 */
public class SmallSizeExample {
  @Test
  public void nobelPrizeProcessScenario() {
    BPMPManager bm = new BPMPManager();
    BPMPElement ncfm = bm.createInstance("p", "-name=ncfm");
    BPMPElement n = bm.createInstance("p", "-name=n");
    BPMPElement e = bm.createInstance("p", "-name=e");
    BPMPElement na = bm.createInstance("p", "-name=na");

    BPMPElement se1 = bm.createInstance("se", "-name=se1", "-in=ncfm");
    BPMPElement snf = bm.createInstance("t", "-name=snf", "-in=ncfm", "-is=se1");
    BPMPElement ccf = bm.createInstance("t", "-name=ccf", "-in=ncfm", "-is=snf");
    BPMPElement sspc = bm.createInstance("t", "-name=sspc", "-in=ncfm", "-is=ccf");
    BPMPElement dnfea = bm.createInstance("t", "-name=dnfea", "-in=ncfm", "-is=sspc");
    BPMPElement og1 = bm.createInstance("og", "-name=og1", "-in=ncfm", "-is=dnfea");
    BPMPElement slospc = bm.createInstance("t", "-name=slospc", "-in=ncfm", "-is=og1");
    BPMPElement ccwar = bm.createInstance("t", "-name=ccwar", "-in=ncfm", "-is=slospc");
    BPMPElement sfcatw = bm.createInstance("t", "-name=sfcatw", "-in=ncfm", "-is=ccwar");
    BPMPElement wrr = bm.createInstance("t", "-name=wrr", "-in=ncfm", "-is=sfcatw&og1");
    BPMPElement srwr = bm.createInstance("t", "-name=srwr", "-in=ncfm", "-is=wrr");
    BPMPElement hnpac = bm.createInstance("t", "-name=hnpac", "-in=ncfm", "-is=srwr");
    BPMPElement ee1 = bm.createInstance("ee", "-name=ee1", "-in=ncfm", "-is=hnpac");

    BPMPElement se2 = bm.createInstance("se", "-name=se2", "-in=n", "-im=snf");
    BPMPElement ipn = bm.createInstance("t", "-name=ipn", "-in=n", "-is=se2");
    BPMPElement smcf = bm.createInstance("t", "-name=smcf", "-in=n", "-is=ipn");
    BPMPElement ee2 = bm.createInstance("ee", "-name=ee2", "-in=n", "-is=smcf");

    BPMPElement se3 = bm.createInstance("se", "-name=se3", "-in=e", "-im=slospc");
    BPMPElement acw = bm.createInstance("t", "-name=acw", "-in=e", "-is=se3");
    BPMPElement scar = bm.createInstance("t", "-name=scar", "-in=e", "-is=acw");
    BPMPElement ee3 = bm.createInstance("ee", "-name=ee3", "-in=e", "-is=scar");

    BPMPElement se4 = bm.createInstance("se", "-name=se4", "-in=na", "-im=srwr");
    BPMPElement dn = bm.createInstance("t", "-name=dn", "-in=na", "-is=se4");
    BPMPElement sl = bm.createInstance("t", "-name=sl", "-in=na", "-is=dn");
    BPMPElement anpl = bm.createInstance("t", "-name=anpl", "-in=na", "-is=sl");
    BPMPElement ee4 = bm.createInstance("ee", "-name=ee4", "-in=na", "-is=anpl");

    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(se1, snf).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(snf, ccf).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(ccf, sspc).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(sspc, dnfea).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(dnfea, og1).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(og1, slospc).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(og1, wrr).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(slospc, ccwar).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(ccwar, sfcatw).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(sfcatw, wrr).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(wrr, srwr).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(srwr, hnpac).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(hnpac, ee1).getClass()));

    Iterator<BPMPElement> omfIt = ((Task) snf).iterateOutwardMessageFlows();
    int countOmf = 0;
    while(omfIt.hasNext()) {
      omfIt.next();
      countOmf++;
    }
    Assert.assertEquals(1, countOmf);
    Assert.assertTrue(MessageFlow.class.isAssignableFrom(bm.getLink(snf, se2).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(se2, ipn).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(ipn, smcf).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(smcf, ee2).getClass()));

    Assert.assertTrue(MessageFlow.class.isAssignableFrom(bm.getLink(slospc, se3).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(se3, acw).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(acw, scar).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(scar, ee3).getClass()));

    Assert.assertTrue(MessageFlow.class.isAssignableFrom(bm.getLink(srwr, se4).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(se4, dn).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(dn, sl).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(sl, anpl).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(anpl, ee4).getClass()));

    Iterator<BPMPElement> ncfmstartIt = ((Pool) ncfm).iterateStarts();
    int count1 = 0;
    BPMPElement ncfmstart = null;
    while(ncfmstartIt.hasNext()) {
      ncfmstart = ncfmstartIt.next();
      count1++;
    }
    Assert.assertEquals(count1, 1);
    Assert.assertEquals(se1, ncfmstart);

    Iterator<BPMPElement> nstartIt = ((Pool) n).iterateStarts();
    int count2 = 0;
    BPMPElement nstart = null;
    while(nstartIt.hasNext()) {
      nstart = nstartIt.next();
      count2++;
    }
    Assert.assertEquals(count2, 1);
    Assert.assertEquals(se2, nstart);

    Iterator<BPMPElement> estartIt = ((Pool) e).iterateStarts();
    int count3 = 0;
    BPMPElement estart = null;
    while(estartIt.hasNext()) {
      estart = estartIt.next();
      count3++;
    }
    Assert.assertEquals(count3, 1);
    Assert.assertEquals(se3, estart);

    Iterator<BPMPElement> nastartIt = ((Pool) na).iterateStarts();
    int count4 = 0;
    BPMPElement nastart = null;
    while(nastartIt.hasNext()) {
      nastart = nastartIt.next();
      count4++;
    }
    Assert.assertEquals(count4, 1);
    Assert.assertEquals(se4, nastart);
  }
}
